package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.bean.main.BottomShowBean;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3HomeBottomAdapter extends RecyclerView.Adapter<X3HomeBottomAdapter.HomeBottomHolder> {

    private List<BottomShowBean> mList;

    private Context mContext;

    private SparseArray<Handler> handlerArray;

    private SparseArray<Runnable> runArray;

    public X3HomeBottomAdapter(Context mContext) {
        this.mList = new ArrayList<>();
        this.mContext = mContext;
        handlerArray = new SparseArray<>();
        runArray = new SparseArray<>();
    }

    public void changeBottomList(List<BottomShowBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public HomeBottomHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HomeBottomHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_home_bottom_view,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull HomeBottomHolder holder, int position) {
        // 加载车辆头像
        X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(mList.get(position).getCarBrandImgUrl()),holder.iv_brand,R.mipmap.x3_car_icon_no);

        List<String> serviceBaseNameList = mList.get(position).getServiceBaseNameList();

        if (!X3StringUtils.isListEmpty(serviceBaseNameList)) {
            String serviceName = "";
            for (int i = 0; i < serviceBaseNameList.size(); i++) {
                if (i == 0) {
                    serviceName = serviceBaseNameList.get(i);
                }else {
                    serviceName = serviceName + "," + serviceBaseNameList.get(i);
                }
            }
            holder.tv_car_num.setText(serviceName);

        }else {
            holder.tv_car_num.setText("");
        }

        holder.tv_time.setText(mList.get(position).getCarNum());

        long status = mList.get(position).getStatus();

        String completeTime = mList.get(position).getCompleteTime();
        String nowTime = mList.get(position).getStartTime();
        if (status == 0) {
            // 预约单
            holder.btn_show.setTextColor(Color.parseColor("#B1B3BD"));
            holder.btn_show.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_bottom_status_one));
            holder.btn_show.setText(mContext.getString(R.string.x3_look));

            if (holder.mHandler != null) {
                holder.mHandler.removeCallbacks(holder.runnable);
            }

            holder.mHandler = new Handler();

            holder.runnable = new Runnable() {
                @Override
                public void run() {
                    String homeTimeCute = X3DateUtils.getHomeTimeIn(nowTime,mContext);
                    if ("refresh".equals(homeTimeCute)) {
                        BottomShowBean bottomShowBean = mList.get(position);
                        bottomShowBean.setStatus(1);
                        mList.remove(position);
                        mList.add(position,bottomShowBean);
                        notifyItemChanged(position);

                        holder.mHandler.removeCallbacks(this);
                        if (iOnClickRefresh != null) {
                            iOnClickRefresh.iOnClickRefresh();
                        }
                    }else {
                        if (position < mList.size()) {
                            holder.mHandler.postDelayed(this, 1000);
                        }
                    }
                }
            };

            holder.mHandler.post(holder.runnable);

            handlerArray.put(holder.mHandler.hashCode(),holder.mHandler);
            runArray.put(holder.runnable.hashCode(),holder.runnable);

        } else if (status == 1) {
            // 施工中
            boolean overTimeOrNot = mList.get(position).isOverTimeOrNot();
            if (overTimeOrNot) {
                // 超时
                holder.btn_show.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_bottom_status_two));
                holder.btn_show.setTextColor(Color.parseColor("#F64444"));
                holder.btn_show.setText(mContext.getString(R.string.x3_continue_work));

            } else {
                // 施工中未超时
                holder.btn_show.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_bottom_status_three));
                holder.btn_show.setTextColor(Color.parseColor("#0077FF"));

                holder.btn_show.setText(mContext.getString(R.string.x3_continue_work));

                if (holder.mHandler != null) {
                    holder.mHandler.removeCallbacks(holder.runnable);
                }

                holder.mHandler = new Handler();

                holder.runnable = new Runnable() {
                    @Override
                    public void run() {
                        if (position < mList.size()) {
                            String homeTimeCute = X3DateUtils.getHomeTimeLeft(completeTime,mContext);
                            if ("refresh".equals(homeTimeCute)) {
                                BottomShowBean bottomShowBean = mList.get(position);
                                bottomShowBean.setOverTimeOrNot(true);
                                mList.remove(position);
                                mList.add(position,bottomShowBean);
                                notifyItemChanged(position);

                                holder.mHandler.removeCallbacks(this);
                                if (iOnClickRefresh != null) {
                                    iOnClickRefresh.iOnClickRefresh();
                                }
                            }else {
                                if (position < mList.size()) {
                                    holder.mHandler.postDelayed(this, 1000);
                                }
                            }
                        }
                    }
                };

                holder.mHandler.post(holder.runnable);

                handlerArray.put(holder.mHandler.hashCode(),holder.mHandler);
                runArray.put(holder.runnable.hashCode(),holder.runnable);

            }
        }

        //跳转到检测和施工
        holder.btn_show.setOnClickListener(view -> {
            if (mList.get(position).isIfDetection()) {
                Bundle bundle = new Bundle();
                bundle.putLong("id",mList.get(position).getId());
                bundle.putLong("receiveBaseId",mList.get(position).getReceiveBaseId());
                X3RouterCenter.startNavigate(X3AppRouterConstants.Router.MAIN_TAIN,bundle);
//                RouterCenter.startNavigate("/mechanic/maintain",bundle);

                //去检测报告
//                Intent intent = new Intent(mContext, MainNewTainActivity.class);
//                intent.putExtra("id", mList.get(position).getId());
//                intent.putExtra("receiveBaseId",mList.get(position).getReceiveBaseId());
//                mContext.startActivity(intent);
                return;
            }
            //去保养，去施工
            Bundle bundle = new Bundle();
            bundle.putLong("id",mList.get(position).getId());
            bundle.putLong("receiveBaseId",mList.get(position).getReceiveBaseId());
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.MAIN_SIVE,bundle);

//            Intent intent = new Intent(mContext, MainNewSiveTainActivity.class);
//            intent.putExtra("id", mList.get(position).getId());
//            intent.putExtra("carId",mList.get(position).getCarId());
//            intent.putExtra("receiveBaseId",mList.get(position).getReceiveBaseId());
//            mContext.startActivity(intent);
        });
    }

    public void cancelAllTimers() {
        if (handlerArray != null) {
            int size = handlerArray.size();
            for (int i = 0; i < size; i++) {
                Handler handler = handlerArray.get(handlerArray.keyAt(i));
                Runnable run = runArray.get(runArray.keyAt(i));
                if (handler != null && run != null) {
                    handler.removeCallbacks(run);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    IOnClickRefresh iOnClickRefresh;

    public void setiOnClickRefresh(IOnClickRefresh iOnClickRefresh) {
        this.iOnClickRefresh = iOnClickRefresh;
    }

    public interface IOnClickRefresh {
        void iOnClickRefresh();
    }

    class HomeBottomHolder extends RecyclerView.ViewHolder {
        ImageView iv_brand;
        TextView tv_car_num;
        TextView tv_time;
        Button btn_show;
        private Handler mHandler;
        private Runnable runnable;
        public HomeBottomHolder(@NonNull View itemView) {
            super(itemView);
            iv_brand = itemView.findViewById(R.id.iv_brand);
            tv_car_num = itemView.findViewById(R.id.tv_car_num);
            tv_time = itemView.findViewById(R.id.tv_time);
            btn_show = itemView.findViewById(R.id.btn_show);
        }
    }
}
